# sign获得方式:
#
# MD5（13位时间戳+8位十进制随机数字+版本号+signKey）后转16进制大写字符串
#
# 示例:
# var sign = MD5(timestamp + nonce + version + partnerId + signKey).toUpperCase()

import time
import hashlib
import requests
import random, string

timestamp = int(round(time.time() * 1000))
print('13位时间戳为:', timestamp)

nonce = ''.join(random.sample(string.digits, 8))
print('生成的8位随机数', nonce)
version = '1.0'
partnerId = '19'
signKey = 'PB7L5K8CUCLIYRALDR813SRP6BKSPNXQ'

md = hashlib.md5()
md.update((str(timestamp) + nonce + version + partnerId + signKey).encode())
sign = md.hexdigest().upper()
print('计算出的签名sign为:', sign)


#
# url='http://partnerloan.tt.sanwenqian.cn/feedback/register'
# data= {
#     "fields": {
#       "mobile": "17610769836",
#       "remark": "",
#       "status": 1
#     },
#     "nonce": nonce,
#     "partnerId": 19,
#     "sign": sign,
#     "timestamp": timestamp,
#     "version": version
#   }
#
# res = requests.post(url,json=data)
# print('响应参数:',res.json())


def md5forSign(str):
    import hashlib
    # str = str + SALT # 加盐
    md = hashlib.md5()
    md.update(str.encode())
    res = md.hexdigest()
    return res


str = str(timestamp) + nonce + version + partnerId + signKey
signStr = md5forSign(str).upper()
print('计算出的签名sign为:', signStr)
